package com.dofast.bt.mvp.view.activity.basket;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Address;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.presenter.basket.SubmitOrderPresenter;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;
import com.dofast.bt.widget.MyListView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dofast.bt.mvp.view.activity.basket.EditAddressActivity.ADDRESS;

/**
 * username:Created by hsj on 2016/12/18 0018.
 * class：
 * 类描述：提交訂單
 */

public class SubmitOderActivity extends BaseMvpActivity<SubmitOrderPresenter, SubmitOderView> implements SubmitOderView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_address)
    ImageView ivAddress;
    @BindView(R.id.iv_selcect)
    ImageView ivSelcect;
    @BindView(R.id.tv_name_s)
    TextView tvNameS;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_address_s)
    TextView tvAddressS;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.iv_go)
    ImageView ivGo;

    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.lv_goods)
    MyListView lvGoods;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.tv_pay_way)
    TextView tvPayWay;
    @BindView(R.id.tv_pay_online)
    TextView tvPayOnline;
    public static final String SELECT_ADDRESS =SubmitOderActivity.class.getSimpleName();
    public static final String ADDRESS =  "secect_address";
    private IntentFilter filter;
    private MyBroadercast myBroadercast;
    @Override
    protected int getResourcesId() {
        return R.layout.activity_payment;
    }

    @Override
    protected void initView() {
        initTitle();

        ivGo.setVisibility(View.VISIBLE);
        ivAddress.setVisibility(View.VISIBLE);
        ivSelcect.setVisibility(View.INVISIBLE);
        tvPayOnline.setVisibility(View.GONE);
        tvPayWay.setVisibility(View.GONE);
        tvCommit.setText("提交订单");
        presenter.initData();
        initBrodercast();

    }
    //初始化广播
    private void initBrodercast() {
        filter = new IntentFilter();
        filter.addAction(SELECT_ADDRESS);
        myBroadercast = new MyBroadercast();
        registerReceiver(myBroadercast, filter);
    }
    @Override
    protected void addOnClickListener() {

    }

    @Override
    public Loader<SubmitOrderPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<SubmitOrderPresenter>() {
            @Override
            public SubmitOrderPresenter create() {
                return new SubmitOrderPresenter();
            }
        });
    }

    @Override
    public void initAdapter(Adapter adapter) {
        lvGoods.setAdapter((ListAdapter) adapter);
    }

    @Override
    public void showDiaolog() {

    }

    public static void gotoSubmitOrderActivity(Context context, List<ShoppingCartBean> goods) {
        Intent intent = new Intent(context, SubmitOderActivity.class);
        intent.putExtra("goods", (Serializable) goods);
        context.startActivity(intent);
    }


    @Override
    public List<ShoppingCartBean> getGoods() {
        return (List<ShoppingCartBean>) getIntent().getSerializableExtra("goods");
    }

    @Override
    public AddressBean getAddress() {
        AddressBean addressBean = new AddressBean();
        addressBean.setName(tvName.getText().toString().trim());
        addressBean.setAddress(tvAddress.getText().toString().trim());
        addressBean.setPhone(tvPhone.getText().toString().trim());
        return addressBean;
    }

    @Override
    public void setAddress(AddressBean address) {
        tvAddress.setText(address.getAddress());
        tvName.setText(address.getName());
        tvPhone.setText(address.getPhone());
    }

    @Override
    public void setAllMoney(String allMoney) {
        tvAllMoney.setText(allMoney);
    }

    @Override
    public void changeAddress() {
        EditAddressActivity.gotoEditAddressActivity(SubmitOderActivity.this);
    }

    @Override
    public void gotoPay(AddressBean addressBean, List<ShoppingCartBean> goods) {
        PaymentActivity.gotoPaymentActivity(SubmitOderActivity.this, addressBean, goods);
    }




    @Override
    public void initTitle() {
        tvTitle.setText("提交订单");
        tvEdit.setVisibility(View.GONE);
        ivBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.iv_go, R.id.ll_address, R.id.tv_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_go:
                break;
            case R.id.ll_address:
                // TODO: 2016/12/18 0018 添加地址逻辑处理
                presenter.changeAddress();
                break;
            case R.id.tv_commit:
                // TODO: 2016/12/18 0018 提交订单逻辑处理
                presenter.pay();
                break;
        }
    }

    private class MyBroadercast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            AddressBean addressBean = (AddressBean) intent.getSerializableExtra(ADDRESS);
            presenter.modifyAddress(addressBean);
        }
    }

}
